package bdhub.littletail.lib.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import bdhub.littletail.lib.utils.log.CLog;
import se.emilsjolander.sprinkles.CursorList;
import se.emilsjolander.sprinkles.QueryResult;

import static bdhub.littletail.lib.ui.adapter.BaseAdapterHelper.get;

/**
 * Abstraction class of a BaseAdapter in which you only need
 * to provide the convert() implementation.<br/>
 * Using the provided BaseAdapterHelper, your code is minimalist.
 * @param <T> The type of the items in the list.
 */
public abstract class QuickCursorAdapter<T extends QueryResult> extends BaseAdapter {

	protected static final String TAG = QuickCursorAdapter.class.getSimpleName();

    protected final Context context;

	protected final int layoutResId;

	private CursorList<T> data;

    /**
     * Create a QuickAdapter.
     * @param context     The context.
     * @param layoutResId The layout resource id of each item.
     */
    public QuickCursorAdapter(Context context, int layoutResId) {
        this.context = context;
        this.layoutResId = layoutResId;
    }
    
    public void changeCursor(CursorList<T> cursorList) {
    	CursorList<T> oldData = data;
    	data = cursorList;
		notifyDataSetChanged();
		if (null != oldData) {
			oldData.close();
		}
	}

    @Override
    public int getCount() {
    	int dataSize = data == null ? 0 : data.size();
        return dataSize;
    }

    @Override
    public T getItem(int position) {
        if (position >= data.size()) return null;
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final BaseAdapterHelper helper = getAdapterHelper(position, convertView, parent);
        // 怀疑：有大量的数据存取操作时，cursor 会被自动 close
		// 判断数据是否为空再执行，获取数据时，也判断 cursor 是否 close ，如果 close 则返回空
		// edit by Even. 2015/03/27
        T t = getItem(position);
        if (null != t) {
        	convert(helper, getItem(position));
		} else {
			CLog.show("data is null, stop convert.");
		}
        return helper.getView();
    }

    @Override
    public boolean isEnabled(int position) {
        return position < data.size();
    }
    
    public void release() {
		if (null != data) {
			data.close();
			data = null;
		}
	}

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    protected abstract void convert(BaseAdapterHelper helper, T item);

	/**
	 * You can override this method to use a custom BaseAdapterHelper in order to fit your needs
	 *
	 * @param position The position of the item within the adapter's data set of the item whose view we want.
	 * @param convertView The old view to reuse, if possible. Note: You should check that this view
	 *        is non-null and of an appropriate type before using. If it is not possible to convert
	 *        this view to display the correct data, this method can create a new view.
	 *        Heterogeneous lists can specify their number of view types, so that this View is
	 *        always of the right type (see {@link #getViewTypeCount()} and
	 *        {@link #getItemViewType(int)}).
	 * @param parent The parent that this view will eventually be attached to
	 * @return An instance of BaseAdapterHelper
	 */
	protected BaseAdapterHelper getAdapterHelper(int position, View convertView, ViewGroup parent) {
		return get(context, convertView, parent, layoutResId, position);
	}

}
